package com.cn.mmd.controller;


import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import com.cn.mmd.tools.GetLimitNum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 最高管理员对权限的操作
 * Created by Administrator on 2016/8/12 0012.
 */
@Controller
@RequestMapping(value = "page/manageShiro")
public class ManageShiroController {

    @Autowired
    private TeacherDataLService teacherDataLService;

    @Autowired
    private ShiroUserRoleService shiroUserRoleService;

    @Autowired
    private ShiroPermissionService shiroPermissionService;

    @Autowired
    private LogManageService logManageService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ShiroRoleService shiroRoleService;


    @Autowired
    private LogController logController;

    /**
     * 给人分配角色
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/addTeaRole")
    @ResponseBody
    public synchronized Object  addShiro(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String teacherId = request.getParameter("teacherId");
        String roleId1 = request.getParameter("roleId");
        String manageId = request.getParameter("manageId");
        if (roleId1!=null&&!roleId1.trim().equals("")) {
            Integer roleId = Integer.parseInt(roleId1);
            if (teacherDataLService.selectByPrimaryKey(teacherId) != null) {
                ShiroUserRole shiroUserRole = new ShiroUserRole();
                shiroUserRole.setRoleId(roleId);
                shiroUserRole.setUserId(teacherId);
                if (shiroUserRoleService.selectByAll(shiroUserRole) == null) {
                    if (roleId<8&&roleId>4){
                        ShiroUserRole shiroUserRole1 = new ShiroUserRole();
                        shiroUserRole1.setRoleId(5);
                        shiroUserRole1.setUserId(teacherId);
                        ShiroUserRole shiroUserRole2 = new ShiroUserRole();
                        shiroUserRole2.setRoleId(6);
                        shiroUserRole2.setUserId(teacherId);
                        ShiroUserRole shiroUserRole3 = new ShiroUserRole();
                        shiroUserRole3.setRoleId(7);
                        shiroUserRole3.setUserId(teacherId);
                        if (shiroUserRoleService.selectByAll(shiroUserRole1) != null){
                            shiroUserRole1 = shiroUserRoleService.selectByAll(shiroUserRole1);
                            ShiroUserRole shiroUserRole4 = new ShiroUserRole();
                            shiroUserRole4.setRoleId(roleId);
                            shiroUserRole4.setId(shiroUserRole1.getId());
                            if (shiroUserRoleService.updateByPrimaryKeySelective(shiroUserRole4)>0){
                                objectMap.put("code", 200);
                                objectMap.put("message", "修改成功");
                                Date date = new Date();
                                SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                                String logTime = time.format(date);
                                LogManage logManage = new LogManage();
                                logManage.setLogtime(logTime);
                                logManage.setManageid(manageId);
                                logManage.setLogcontent("给" + teacherId + "修改了一个" + roleId + "角色");
                                logManageService.insertSelective(logManage);
                            }else {
                                objectMap.put("code", 201);
                                objectMap.put("message", "添加失败");
                            }
                        }else if (shiroUserRoleService.selectByAll(shiroUserRole2) != null){
                            shiroUserRole2 = shiroUserRoleService.selectByAll(shiroUserRole2);
                            ShiroUserRole shiroUserRole4 = new ShiroUserRole();
                            shiroUserRole4.setRoleId(roleId);
                            shiroUserRole4.setId(shiroUserRole2.getId());
                            if (shiroUserRoleService.updateByPrimaryKeySelective(shiroUserRole4)>0){
                                objectMap.put("code", 200);
                                objectMap.put("message", "修改成功");
                                Date date = new Date();
                                SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                                String logTime = time.format(date);
                                LogManage logManage = new LogManage();
                                logManage.setLogtime(logTime);
                                logManage.setManageid(manageId);
                                logManage.setLogcontent("给" + teacherId + "修改了一个" + roleId + "角色");
                                logManageService.insertSelective(logManage);
                            }else {
                                objectMap.put("code", 201);
                                objectMap.put("message", "添加失败");
                            }
                        }else if (shiroUserRoleService.selectByAll(shiroUserRole3) != null){
                            shiroUserRole3 = shiroUserRoleService.selectByAll(shiroUserRole3);
                            ShiroUserRole shiroUserRole4 = new ShiroUserRole();
                            shiroUserRole4.setRoleId(roleId);
                            shiroUserRole4.setId(shiroUserRole3.getId());
                            if (shiroUserRoleService.updateByPrimaryKeySelective(shiroUserRole4)>0){
                                objectMap.put("code", 200);
                                objectMap.put("message", "修改成功");
                                Date date = new Date();
                                SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                                String logTime = time.format(date);
                                LogManage logManage = new LogManage();
                                logManage.setLogtime(logTime);
                                logManage.setManageid(manageId);
                                logManage.setLogcontent("给" + teacherId + "修改了一个" + roleId + "角色");
                                logManageService.insertSelective(logManage);
                            }else {
                                objectMap.put("code", 201);
                                objectMap.put("message", "添加失败");
                            }
                        }else {
                            if (shiroUserRoleService.insertSelective(shiroUserRole) > 0) {
                                objectMap.put("code", 200);
                                objectMap.put("message", "成功");
                                Date date = new Date();
                                SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                                String logTime = time.format(date);
                                LogManage logManage = new LogManage();
                                logManage.setLogtime(logTime);
                                logManage.setManageid(manageId);
                                logManage.setLogcontent("给" + teacherId + "分配了一个" + roleId + "角色");
                                logManageService.insertSelective(logManage);
                            } else {
                                objectMap.put("code", 201);
                                objectMap.put("message", "添加失败");
                            }
                        }
                    }else {
                        if (shiroUserRoleService.insertSelective(shiroUserRole) > 0) {
                            objectMap.put("code", 200);
                            objectMap.put("message", "成功");
                            Date date = new Date();
                            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            String logTime = time.format(date);
                            LogManage logManage = new LogManage();
                            logManage.setLogtime(logTime);
                            logManage.setManageid(manageId);
                            logManage.setLogcontent("给" + teacherId + "分配了一个" + roleId + "角色");
                            logManageService.insertSelective(logManage);
                        } else {
                            objectMap.put("code", 201);
                            objectMap.put("message", "添加失败");
                        }
                    }
                } else {
                    objectMap.put("code", 202);
                    objectMap.put("message", "此用户已有此角色");
                }
            } else {
                objectMap.put("code", 203);
                objectMap.put("message", "此用户不存在");
            }
        }else {
            objectMap.put("code", 204);
            objectMap.put("message", "请认真选择");
        }
        return objectMap;
    }

    /**
     * 修改教师角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/updTeaRole")
    @ResponseBody
    public  synchronized  Object updShiro(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        Integer id = Integer.parseInt(request.getParameter("id"));
        String teacherId = request.getParameter("teacherId");
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));
        String manageId = request.getParameter("manageId");
        ShiroUserRole shiroUserRole = new ShiroUserRole();
        shiroUserRole.setUserId(teacherId);
        shiroUserRole.setRoleId(roleId);
        shiroUserRole.setId(id);
        if (shiroUserRoleService.updateByPrimaryKeySelective(shiroUserRole) > 0) {
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime = time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("id:" + id + ";" + teacherId + "被修改了一个" + roleId + "角色");
            logManageService.insertSelective(logManage);
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "修改失败");
        }
        return objectMap;
    }

    /**
     * 删除教师角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/delTeaRole")
    @ResponseBody
    public synchronized  Object delShiro(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String teacherId = request.getParameter("teacherId");
        if (shiroUserRoleService.deleteByUserId(teacherId) > 0) {
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime = time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("删除了"+teacherId+"的权限");
            logManageService.insertSelective(logManage);
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "删除失败");
        }
        return objectMap;
    }

    /**
     * 查看教师和教师的角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/selAllTeaRole")
    @RequiresPermissions("magger:create")
    @ResponseBody
    public synchronized  Object selAllShiro(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String, Object> map;
        Integer startrow = Integer.parseInt(request.getParameter("startrow"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String teacherId = request.getParameter("teacherId");
        Teacher teacher = new Teacher();
        teacher.setTeacherposition("离职");
        teacher.setStartrow(startrow);
        teacher.setPageSize(pageSize);
        teacher.setTeacherid(teacherId);
        List<Teacher> teacherList = teacherService.selectByPaging(teacher);
        if (teacherList != null && teacherList.size() > 0) {
            List<ShiroUserRole> shiroUserRoleList;
            for (int i = 0 ; i < teacherList.size() ; i ++){
                teacher = teacherList.get(i);
                map = new HashMap<String, Object>();
                shiroUserRoleList = shiroUserRoleService.selectByUserId(teacher.getTeacherid());
                if (shiroUserRoleList!=null&&shiroUserRoleList.size()>0){
                    ShiroUserRole shiroUserRole;
                    String s = "";
                    String s1= null;
                    for (int j = 0 ; j < shiroUserRoleList.size() ; j ++){
                        shiroUserRole = shiroUserRoleList.get(j);
                        if(shiroUserRole.getRoleId()==1){
                            s1= "学生";
                        }else if (shiroUserRole.getRoleId()==2){
                            s1= "教师";
                        }else if (shiroUserRole.getRoleId()==3){
                            s1= "管理员";
                        }else if (shiroUserRole.getRoleId()==4){
                            s1= "领导";
                        }else if (shiroUserRole.getRoleId()==5){
                            s1= "校长";
                        }else if (shiroUserRole.getRoleId()==6){
                            s1= "院长";
                        }else if (shiroUserRole.getRoleId()==7){
                            s1= "课程组长";
                        }
                        s = s1+","+s;
                    }
                    map.put("roles",s);
                }
                map.put("teacherId",teacher.getTeacherid());
                map.put("teacherName",teacher.getTeachername());
                map.put("teacherIdCard",teacher.getTeacheridcard());
                map.put("teacherSchool",teacher.getTeacherschool());
                map.put("teacherCollege",teacher.getTeachercollege());
                map.put("teacherSex",teacher.getTeachersex());
                mapList.add(map);
            }
            if (mapList!=null&&mapList.size() == teacherList.size()) {
                int n = teacherService.selectCount("离职");
                objectMap.put("total",n);
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                objectMap.put("data", mapList);
            }else {
                objectMap.put("total",0);
                objectMap.put("code", 201);
                objectMap.put("message", "查看失败");
                objectMap.put("data", mapList);
            }
        } else {
            objectMap.put("total",0);
            objectMap.put("code", 202);
            objectMap.put("message", "此用户没有拥有角色");
            objectMap.put("data", mapList);
        }
        return objectMap;
    }

    /**
     * 查看全部角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/selAllRole")
    @ResponseBody
    public synchronized  Object selAllRole(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String, Object> map;
        ShrioRole shrioRole = new ShrioRole();
        List<ShrioRole> shrioRoleList = shiroRoleService.selectByPaging(shrioRole);
        if (shrioRoleList!=null&&shrioRoleList.size()>0){
            List<ShiroPermission> shiroPermissionList;
            for (int i = 0 ; i < shrioRoleList.size() ; i ++){
                shrioRole = shrioRoleList.get(i);
                map = new HashMap<String, Object>();
                map.put("roleId",shrioRole.getRoleId());
                map.put("roleName",shrioRole.getRoleName());
                shiroPermissionList =shiroPermissionService.selectByRoleId(shrioRole.getRoleId());
                String s = "";
                if (shiroPermissionList!=null&&shiroPermissionList.size()>0){
                    for (int j = 0 ; j < shiroPermissionList.size() ; j++){
                      s = shiroPermissionList.get(j).getPermissionName()+","+s;
                    }
                }
                map.put("rolePermission",s);
                mapList.add(map);
            }
            if (mapList!=null&&mapList.size()==shrioRoleList.size()){
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                objectMap.put("data", mapList);
            }else {
                objectMap.put("code", 201);
                objectMap.put("message", "失败");
                objectMap.put("data", mapList);
            }
        }else {
            objectMap.put("code", 202);
            objectMap.put("message", "当前并没有设置角色");
            objectMap.put("data", mapList);
        }
        return objectMap;
    }

    /**
     * 添加角色权限
     * @param request
     * @return
     */
    @RequestMapping(value = "/addPermission")
    @ResponseBody
    public synchronized  Object addPermission(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));
        String permission = request.getParameter("permission");
        ShiroPermission shiroPermission = new ShiroPermission();
        shiroPermission.setRoleId(roleId);
        shiroPermission.setPermissionName(permission);
        if (shiroPermissionService.insertSelective(shiroPermission)>0){
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime = time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("添加了一个角色的权限");
            logManageService.insertSelective(logManage);
        }else {
            objectMap.put("code", 201);
            objectMap.put("message", "失败");
        }
        return objectMap;
    }

    /**
     * 删除角色权限
     * @param request
     * @return
     */
    @RequestMapping(value = "/delPermission")
      @ResponseBody
      public synchronized  Object delPermission(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        Integer id = Integer.parseInt(request.getParameter("id"));
        if (shiroPermissionService.deleteByPrimaryKey(id)>0){
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime = time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("删除了一个角色的权限");
            logManageService.insertSelective(logManage);
        }else {
            objectMap.put("code", 201);
            objectMap.put("message", "删除失败");
        }
        return objectMap;
    }

    /**
     * 修改角色权限
     * @param request
     * @return
     */
    @RequestMapping(value = "/updPermission")
    @ResponseBody
    public synchronized  Object updPermission(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        Integer id = Integer.parseInt(request.getParameter("id"));
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));
        String permission = request.getParameter("permission");
        ShiroPermission shiroPermission = new ShiroPermission();
        shiroPermission.setId(id);
        shiroPermission.setRoleId(roleId);
        shiroPermission.setPermissionName(permission);
        if (shiroPermissionService.updateByPrimaryKey(shiroPermission)>0){
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            logController.logManage("修改了 id:"+id+"的权限",manageId);
        }else {
            objectMap.put("code", 200);
            objectMap.put("message", "失败");
        }
        return objectMap;
    }

}
